package com.kymatrix.mpo.plus.bpm.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.kymatrix.mpo.plus.common.util.CommonStatusEnum;
import com.kymatrix.mpo.plus.bpm.server.dao.definition.BpmUserGroupMapper;
import com.kymatrix.mpo.plus.bpm.server.model.entity.definition.BpmUserGroupEntity;
import com.kymatrix.mpo.plus.bpm.server.model.form.group.PageQueryBpmUserGroupReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.group.SaveBpmUserGroupReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.group.UpdateBpmUserGroupReq;
import com.kymatrix.mpo.plus.bpm.server.service.BpmUserGroupService;
import com.kymatrix.mpo.plus.common.business.common.PageResp;
import com.kymatrix.mpo.plus.common.exception.BusinessException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static com.kymatrix.mpo.plus.common.util.CollectionUtils.convertMap;
import static com.kymatrix.mpo.plus.bpm.server.model.enums.ErrorCodeConstants.USER_GROUP_IS_DISABLE;
import static com.kymatrix.mpo.plus.bpm.server.model.enums.ErrorCodeConstants.USER_GROUP_NOT_EXISTS;

/**
 * 用户组 Service 实现类
 */
@Service
@Validated
public class BpmUserGroupServiceImpl implements BpmUserGroupService {

    @Resource
    private BpmUserGroupMapper userGroupMapper;

    @Override
    public String createUserGroup(SaveBpmUserGroupReq req) {
        BpmUserGroupEntity entity = new BpmUserGroupEntity();
        entity.setName(req.getName());
        entity.setDescription(req.getDescription());
        entity.setStatus(req.getStatus());
        entity.setUserIds(req.getUserIds());
        userGroupMapper.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateUserGroup(UpdateBpmUserGroupReq req) {
        // 校验存在
        validateUserGroupExists(req.getId());
        BpmUserGroupEntity entity = this.userGroupMapper.selectById(req.getId());
        entity.setName(req.getName());
        entity.setDescription(req.getDescription());
        entity.setStatus(req.getStatus());
        entity.setUserIds(req.getUserIds());
        userGroupMapper.updateById(entity);
    }

    @Override
    public void deleteUserGroup(String id) {
        userGroupMapper.deleteById(id);
    }

    private void validateUserGroupExists(String id) {
        if (userGroupMapper.selectById(id) == null) {
            throw new BusinessException(USER_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public BpmUserGroupEntity getUserGroup(String id) {
        return userGroupMapper.selectById(id);
    }

    @Override
    public List<BpmUserGroupEntity> getUserGroupList(Collection<String> ids) {
        return userGroupMapper.selectBatchIds(ids);
    }


    @Override
    public List<BpmUserGroupEntity> getUserGroupListByStatus(String status) {
        return userGroupMapper.selectListByStatus(status);
    }

    @Override
    public PageResp<BpmUserGroupEntity> getUserGroupPage(PageQueryBpmUserGroupReq req) {
        return userGroupMapper.selectPage(req);
    }

    @Override
    public void validUserGroups(Collection<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        // 获得用户组信息
        List<BpmUserGroupEntity> userGroups = userGroupMapper.selectBatchIds(ids);
        Map<String, BpmUserGroupEntity> userGroupMap = convertMap(userGroups, BpmUserGroupEntity::getId);
        // 校验
        ids.forEach(id -> {
            BpmUserGroupEntity userGroup = userGroupMap.get(id);
            if (userGroup == null) {
                throw new BusinessException(USER_GROUP_NOT_EXISTS);
            }
            if (!CommonStatusEnum.ENABLE.getStatus().equals(userGroup.getStatus())) {
                throw new BusinessException(USER_GROUP_IS_DISABLE, userGroup.getName());
            }
        });
    }

}
